<?php

namespace App\middlewares;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
 *
 */
class CallRecord
{
    private $db;
    private $msg;

    public function __construct($ci,$msg)
    {
      $this->db = $ci->get('db');
      $this->msg = $msg;
    }

    public function __invoke(ServerRequestInterface $request,ResponseInterface $response,callable $next)
    {
      $token = isset($request->getQueryParams()['token'])?$request->getQueryParams()['token']:null;
      $ip = $_SERVER["REMOTE_ADDR"];
      $this->db->insert('interface_call_record',[
        'access_token' => $token,
        'request_ip' => $ip,
        'msg' => $this->msg
      ]);
      $response = $next($request,$response);
      return $response;
    }
}
